package com.vueblog.webapi.controller;

import com.vueblog.webapi.enums.ResponseEnum;
import com.vueblog.webapi.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;

import java.time.LocalDate;
import java.util.UUID;

/**
 * Created with IDEA
 * author: PengYi
 * Date:2019/9/5
 * Time:9:03
 */
@SuppressWarnings("ResultOfMethodCallIgnored")
@Slf4j
public class BaseController {

    /**
     * 检测BindingResult是否有错误,有错误时抛出异常
     *
     * @param bindingResult BindingResult
     */
    protected void checkBindingResult(BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(ResponseEnum.BAD_REQUEST.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
    }

    protected void checkUuid(String str) {
        try {
            UUID.fromString(str);
        } catch (Exception e) {
            throw new BusinessException(ResponseEnum.BAD_REQUEST);
        }
    }

    protected LocalDate parseToDate(String str) {
        try {
            return LocalDate.parse(str);
        } catch (Exception e) {
            throw new BusinessException(ResponseEnum.BAD_REQUEST);
        }
    }
}
